package com.amigo.online.provider.manager.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.provider.manager.model.entity.Comment;
import com.amigo.online.provider.manager.model.entity.Video;
import com.amigo.online.provider.manager.model.entity.VideoRecommend;
import com.amigo.online.provider.manager.service.VideoCommentService;
import com.amigo.online.provider.manager.service.VideoRecommendService;
import com.amigo.online.provider.manager.service.VideoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@RestController
@RequestMapping(value = "/video", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class VideoController extends BaseController {

    @Resource
    private VideoService videoService;

    @Resource
    private VideoRecommendService videoRecommendService;

    /**
     * 查询视频
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/getvideoList")
    public ResponseEntity<Response> getUserList(@RequestBody Video video) {
        JSONObject result = new JSONObject();
        // 分页语句
        Page<Object> page = PageHelper.startPage(video.getPageNo(), video.getPageCount());
        List<Video> list = videoService.findVideoList(video);
        result.put("videolist", list);
        // 总记录数
        result.put("pagetotal", page.getTotal());
        return success(result);
    }

    /**
     * 查询用户视频
     *
     * @param video
     * @return
     */
    @PostMapping(value = "/getuservideoList")
    public ResponseEntity<Response> getuservideoList(@RequestBody Video video) {
        JSONObject result = new JSONObject();
        // 分页语句
        Page<Object> page = PageHelper.startPage(video.getPageNo(), 12);
        List<Video> list = videoService.findUserVideoList(video);
        result.put("uservideolist", list);
        // 总记录数
        result.put("pagetotal", page.getTotal());
        return success(result);
    }

    /**
     * url添加视频
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/saveVideo")
    public ResponseEntity<Response> saveVideo(@RequestBody Video video) {
        return success(videoService.saveVideo(video));

    }

    @PutMapping(value = "/updateVideo")
    public ResponseEntity<Response> updateVideo(@RequestBody Video video) {
        return success(videoService.updateVideo(video));

    }

    /**
     * 添加本地视频
     *
     * @param video
     * @return
     * @throws InterruptedException
     * @throws IOException
     */
    @PostMapping(value = "/saveLocalVideo")
    public ResponseEntity<Response> saveLocalVideo(@RequestBody Video video) throws IOException, InterruptedException {
        return success(videoService.saveLocalVideo(video));

    }

    /**
     * 删除视频动态
     *
     * @param video
     * @return
     */
    @DeleteMapping(value = "/deleteVideo")
    public ResponseEntity<Response> deleteVideo(@RequestBody Video video) {
        return success(videoService.deleteVideo(video));
    }

    /**
     * 添加热门视频
     *
     * @param video
     * @return
     */
    @PostMapping(value = "/saveHotVideo")
    public ResponseEntity<Response> saveHotVideo(@RequestBody Video video) {
        return success(videoService.saveHotVideo(video));
    }

    /**
     * 添加热门视频 videoRecommend已经弃用但是暂时删除此代码
     *
     * @param videoRecommend
     * @return
     */
    @PostMapping(value = "/saveVideoRecommend")
    public ResponseEntity<Response> saveVideoRecommend(@RequestBody VideoRecommend videoRecommend) {

        JSONObject result = new JSONObject();
        List<VideoRecommend> vr = videoRecommendService.findVideoRecommended(videoRecommend);
        if (!vr.isEmpty() || vr.size() != 0) {
            result.put("msg", "repetition_add");
            return success(result);
        } else {
            return success(videoRecommendService.saveVideoRecommend(videoRecommend));
        }

    }

    /**
     * 查询选择的热门视频 videoRecommend已经弃用但是暂时删除此代码
     *
     * @param videoRecommend
     * @return
     */
    @PostMapping(value = "/getVideoRecommend")
    public ResponseEntity<Response> getVideoRecommend(@RequestBody VideoRecommend videoRecommend) {
        JSONObject result = new JSONObject();
        // 分页语句
        Page<Object> page = PageHelper.startPage(videoRecommend.getPageNo(), 12);
        List<VideoRecommend> list = videoRecommendService.findVideoRecommended(videoRecommend);
        result.put("videorecommendlist", list);
        // 总记录数
        result.put("pagetotal", page.getTotal());

        return success(result);

    }

    /**
     * videoRecommend表已经弃用 弃用
     *
     * @param videoRecommend
     * @return
     */
    @PutMapping(value = "/updateVideoRecommend")
    public ResponseEntity<Response> updateVideoRecommend(@RequestBody VideoRecommend videoRecommend) {

        return success(videoRecommendService.updateVideoRecommend(videoRecommend));
    }

    /**
     * 删除视频 videoRecommend已经弃用但是暂时删除此代码
     *
     * @param dto
     * @return
     */
    @DeleteMapping(value = "/deleteVideoRecommend")
    public ResponseEntity<Response> deleteVideoRecommend(@RequestBody VideoRecommend videoRecommend) {
        return success(videoRecommendService.deleteVideoRecommend(videoRecommend));

    }

    @Autowired
    private VideoCommentService videoCommentService;

    /**
     * 获得视频评论
     *
     * @param videoId
     * @return
     */
    @GetMapping(value = "/getVideoCommentList/{videoId}/{page}")
    public ResponseEntity<Response> getCommentList(@PathVariable("videoId") Long videoId,
                                                   @PathVariable("page") Integer page) {
        return success(videoCommentService.findByVideoId(videoId, page));
    }

    @PostMapping("/disappear")
    public ResponseEntity<Response> disappearCommentById(@RequestBody Comment comment) {
        return success(videoCommentService.disappearComment(comment.getId(), comment.getStatus()));
    }

    /**
     * 查询热门视频
     *
     * @param video
     * @return
     */
    @PostMapping("/getRecommendVideo")
    public ResponseEntity<Response> getRecommendVideo(@RequestBody Video video) {

        JSONObject result = new JSONObject();
        // 分页语句
        Page<Object> page = PageHelper.startPage(video.getPageNo(), 12);
        List<Video> list = videoService.findRecommendVideoList(video);
        result.put("videoRecommendlist", list);
        // 总记录数
        result.put("pagetotal", page.getTotal());
        return success(result);

    }

    @PutMapping("/deleteRecommendVideo")
    public ResponseEntity<Response> deleteRecommendVideo(@RequestBody Video video) {
        return success(videoService.deleteRecommendVideo(video));
    }

}
